﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using POSSystem.DAL;

namespace POSSystem.BLL
{
    public class Contracts : _Contracts
    {
        const string NETWORK = "Network";
        const string PRODUCT = "Product";

        public Contracts()
        {
           
        }

        public override void AddNew()
        {
            base.AddNew();
            if(this.DataTable.Columns[NETWORK] == null)
                this.AddColumn(NETWORK, typeof(string));
            if (this.DataTable.Columns[PRODUCT] == null)
                this.AddColumn(PRODUCT, typeof(string));
        }

        public string Product
        {
            get { return this.GetColumn(PRODUCT).ToString(); }
            set { this.SetColumn(PRODUCT, value); }
        }

        public string Network
        {
            get { return this.GetColumn(NETWORK).ToString(); }
            set { this.SetColumn(NETWORK, value); }
        }

        public static void UpdateMemo(int contractID, string memo)
        {
            try
            {
                Contracts ct = new Contracts();
                ct.LoadByPrimaryKey(contractID);
                ct.Memo = memo;

                ct.Save();
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("Contracts.cs", "UpdateMemo", ex.StackTrace);
            }
        }

        public static string DrawMemo(string memo, string userName)
        {
            string rs = "<table><tr>";
            rs += string.Format("<td style='width:80px; text-align:left; vertical-align:top'>{0}</td>", DateTime.Now.ToShortDateString());
            rs += string.Format("<td style='width:250px; text-align:left; vertical-align:top'>{0}</td>", memo);
            rs += string.Format("<td style='width:100px; text-align:left; vertical-align:top'>{0}</td>", userName);
            rs += "</tr></table>";

            return rs;
        }

        public static Contracts GetContractByInvoiceID(int id)
        {
            try
            {
                Contracts ct = new Contracts();
                ct.Where.InvoiceID.Value = id;
                ct.Where.Deleted.Value = false;
                ct.Query.Load();

                return ct;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("Contracts.cs", "UpdateMemo", ex.Message + " " + ex.StackTrace);
                throw ex;
            }
        }
    }
}
